package org.zuel.boot.demo.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.zuel.boot.demo.web.*;
import org.zuel.boot.demo.exception.ParameterException;
import org.zuel.boot.demo.entity.DemoUser;
import org.zuel.boot.demo.mapper.DemoUserMapper;
import org.zuel.boot.demo.service.IDemoUserService;
import org.springframework.stereotype.Service;

import java.util.Arrays;


/**
 *  服务实现类
 *
 * @author ikun
 */
@Service
public class DemoUserServiceImpl extends ServiceImpl<DemoUserMapper, DemoUser> implements IDemoUserService {

    @Override
    public PageResult<DemoUser> list(PageParam<DemoUser> page) {
        return new PageResult<>(this.page(page).getRecords(), page.getTotal());
    }

    @Override
    public JsonResult read(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        DemoUser demoUser = baseMapper.selectById(id);
        return JsonResult.ok().setData(demoUser);
    }

    @Override
    public JsonResult add(DemoUser demoUser) {
        if (demoUser ==null){
            throw new ParameterException("参数对象为空");
        }

        baseMapper.insert(demoUser);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult update(DemoUser demoUser) {
        if (demoUser == null) {
            throw new ParameterException("参数对象为空");
        }

        if (demoUser.getId() == null) {
            throw new ParameterException("参数对象id为空");
        }

        baseMapper.updateById(demoUser);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult delete(Integer[] ids) {
        if (ids == null) {
            throw new ParameterException("参数对象为空");
        }

        if (ids != null || ids.length > 0) {
            baseMapper.deleteBatchIds(Arrays.asList(ids));
        }
        return JsonResult.ok("删除成功");
    }

}